/**
 * 
 */
package server;

import java.util.Vector;

import client.ClientEvent;
import client.ClientListener;

/**
 * @author Sefverl
 *
 */
public class EventManager 
{
	private static EventManager manager;
	private static Vector<ClientListener> listenerList = new Vector<ClientListener>();
	public static final int CONNECTED = 105, DISCONNECTED = 210, AUTHENTICATED = 315;
	
	/**
	 * Returns an instance of EventManager
	 * @return 
	 */
	public static EventManager getInstance()
	{
		if(manager == null)	manager = new EventManager();
		
		return manager;
	}	
	
	public void addClientListener(ClientListener l)
	{
		listenerList.add(l);
	}
	
	public void removeClientListener(ClientListener l)
	{
		listenerList.remove(l);
	}
	
	/**
	 * Fire event depending on its type
	 * @param o event info object
	 * @param type type of event
	 */
	public void fireEvent(Object source,ClientObject o,int type)
	{
		ClientEvent event = new ClientEvent(source,o);
		System.out.println("I am working @ fireEvent before Loop");
		
		for(ClientListener listener : listenerList)
		{
			launchEventType(listener, type, event);
			System.out.println("I am working @ fireEvent in Loop");
		}
		
		System.out.println("I am working @ fireEvent after Loop");
	}
	
	/**
	 * Call method from ClientListener depending on event type
	 * 
	 * @param l ClientListener from which to call method from
	 * @param type type of event
	 * @param o event info object
	 */
	private void launchEventType(ClientListener l, int type, ClientEvent event)
	{
		System.out.println("I am working @ launchEventType before switch");
		switch(type)
		{
		case CONNECTED:
			l.clientConnected(event);
			System.out.println("You called CONNECTED");
			break;
		case DISCONNECTED:
			l.clientDisconnected(event);
			System.out.println("You called DISCONNECTED");
			break;
		case AUTHENTICATED:
			l.clientAuthenticated(event);
			System.out.println("You called AUTHENTICATED");
			break;
			
			default:
			l.ClientEventPerformed(event); 
			System.out.println("You called DEFAULT");
		}
	}
	
}
